/*浏览器不管读到的是什么都会按照html返回，因此导致错误，所以我们要写if语句自己判断读取到的是什么*/

//导入模块
const http = require('http')
const fs = require('fs');
const { isBuffer } = require('util');
//创建服务对象
const server = http.createServer((request, response) => {
    //读取URL的请求路径
    let { pathname } = new URL(request.url, 'http://127.0.0.1');
    //设置响应体
    if (pathname === '/') {
        //读取文件内容
        let html = fs.readFileSync(__dirname + '/table.html');
        response.end(html);
    } else if (pathname === '/index.css') {
        let css = fs.readFileSync(__dirname + '/index.css');
        response.end(css);
    } else if (pathname === '/index.js') {
        let js = fs.readFileSync(__dirname + '/index.js');
        response.end(js);
    } else {
        response.statusCode = 404;
        response.end('<h1>404 Not Found</h1>');
    }
    // response.end(__dirname)
})
//监听端口
server.listen(9000, () => {
    console.log('服务已经启动……');
})